//
//  YDHttpClient.m
//  IngCard
//
//  Created by kf on 2017/8/23.
//  Copyright © 2017年 IngDan. All rights reserved.
//

#import "YDHttpClient.h"
#import "PPNetworkHelper.h"

@implementation YDHttpClient
+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    static YDHttpClient *instance = nil;
    dispatch_once(&onceToken,^{
        instance = [[super allocWithZone:NULL] init];
    });
    return instance;
}

+ (id)allocWithZone:(struct _NSZone *)zone{
    return [self sharedInstance];
}
- (__kindof NSURLSessionTask *)POST:(NSString *)url
                         parameters:(void (^)(NSMutableDictionary * parameters)) parametersConfig
                         onComplete:(Block_OnComplete) onComplete;
{
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    BlockGoGo(parametersConfig, parameters);
    
    Log(@"%@", url);
    Log(@"%@", parameters);
    
    return [PPNetworkHelper POST:url
                      parameters:parameters
                         success:^(id responseObject){
                             NSDictionary *responseDic = NSDictionaryMake(responseObject);
                    
                             Log(@"%@",responseDic);
                             
                             if ([NSStringMake(responseDic[@"status"]) isEqualToString:@"200"])
                             {
                                 dispatch_async_main(^{
                                     BlockGoGo(onComplete,YES,NSDictionaryMake(responseDic[@"data"]),NSStringMake(responseDic[@"msg"]));
                                 })
                             }
                             else
                             {
                                 dispatch_async_main(^{
                                     BlockGoGo(onComplete,NO,nil,NSStringMake(responseDic[@"msg"]));
                                 })
                             }
                         }
                         failure:^(NSError *error){
                             Log(@"%@",error);
                             dispatch_async_main(^{
                                 BlockGoGo(onComplete,NO,nil,error.description);
                             })
                         }];
}
@end





